﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class CommandTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public CommandTypeDAL()
        {

        }
        public DataSet GetCommandTypeInfo(int Sequence, int Group)
        {
            string strSQL;
            DataSet ds = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);
                dbManager.AddParameters(2, "@Group", Group);

                strSQL = "SELECT *"
                     + " FROM HRM_PS_COMMAND_TYPES"
                     + " WHERE RECORD_STATUS = @ActiveRecord"
                     + (Sequence > 0 ? " AND CMD_TYPE_SEQ = @Sequence" : "")
                     + (Group > 0 ? " AND CMD_GROUP_SEQ = @Group" : "");
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                return ds;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public void UpdateFormat(int Sequence, string Header, string Format, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@Header", Header);
                dbManager.AddParameters(2, "@Format", Format);
                dbManager.AddParameters(3, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(4, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_COMMAND_TYPES"
                    + " SET RPT_HEADER = @Header"
                        + ", RPT_FORMAT = @Format"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE CMD_TYPE_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}
